package com.gosophia.conference.action.notification;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.common.ws.ConferenceQueryWebService;
import com.gosophia.conferenceManagement.notification.dto.NotificationJobDataDTO;
import com.gosophia.conferenceManagement.notification.ws.NotificationWebService;
import com.gosophia.conferenceManagement.scheme.ws.ConferenceSchemeWebService;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;
import com.gosophiaigit.contact.busi.entity.Contact;
import com.gosophiaigit.contact.busi.service.IContactGroupService;
/**
 * 获取通知jobdateAction
 * @author gaoweiguang
 * @since  2011-3-26
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Scope("prototype")
@Controller("GetNotificationJobDateAction")
public class GetNotificationJobDateAction extends BaseSupportAction{

	private static final long serialVersionUID = -1427069474243858424L;
    @Autowired
	private  NotificationWebService notificationWebService;
	@Autowired
	private TaskManagementWebService taskManagementWebService;
	@Autowired
	private ConferenceQueryWebService conferenceQueryWebService;
	@Autowired
	private UserManagementWebService userManagementWebService;
	@Autowired
	private ConferenceSchemeWebService conferenceSchemeWebService;
	@Autowired
	private JudgeCurrentUndertaker judgeCurrentUndertaker;
	@Autowired
	private IContactGroupService iContactGroupService; 
	 
    private NotificationJobDataDTO notificationJobDataDTO;    

	private String sendUserListString;

    private JobDTO jobDTO;

	private Long jobId;
	
	//会议任务详细
    private ConferenceStartingDTO conferenceStartingDTO;
	
    private String onlineIsEdit = null;
	
	private String notificationType;
	
	private String onlinePath;

	private String onlineVersion;

	private List<JobDTO> jobSchemeDTOs;
	//预通知审批记录
	private List<JobDTO> notificationPreHistoryRecord;
	//正式通知审批记录
	private List<JobDTO> notificationHistoryRecord;

	private String nextApprovalListString;
	
	private List<UserInfoDTO> nextApprovalList;
	//会议申请审批记录
	private List<JobDTO> jobDTOs;
	//方案审批记录
    private List<JobDTO> schemeHistoryRecord;
    
    private JobDTO job;
	
	@Override
	public String _execute() throws Exception {
		 //判断是否匹配,如果不匹配转到异常页面
        judgeCurrentUndertaker.judgeOperationAuthority(jobId);
		nextApprovalList = userManagementWebService.findAllUserInfoHasAccount();
		conferenceStartingDTO = conferenceQueryWebService.findConferenceMasterByJobId(jobId);
	    if(nextApprovalList!=null){
	            nextApprovalListString = JSONArray.fromObject(nextApprovalList).toString().replace("\"", "\\\"");
	    }
	    //获取会议申请审批记录
	    jobDTOs = conferenceQueryWebService.findHandleHistory(jobId);
	    //获取方案审批记录
	    schemeHistoryRecord = conferenceSchemeWebService.findFinishedSchemeApprovalHistoryRecordByJobId(jobId);
	    //获取预通知审批记录
		notificationPreHistoryRecord = notificationWebService.findFinishedNotificationApprovalHistoryRecordByJobId(jobId, "0");
		//获取正式通知的审批记录
		notificationHistoryRecord = notificationWebService.findFinishedNotificationApprovalHistoryRecordByJobId(jobId, "1");
		notificationJobDataDTO = notificationWebService.findNotificationJobDataByJobId(jobId);
		NotificationJobDataDTO notificationPreJobDataDTO =  notificationWebService.findPreNotificationJobDataByJobId(jobId);
		job = taskManagementWebService.findJobDetailByJobId(jobId);
		  if(job.getJobType().startsWith("预通知")) {
			  notificationType = "0"; 
		  } else {
			  notificationType = "1"; 
		  }
		if(notificationJobDataDTO==null) {
			if(notificationPreJobDataDTO==null) {
				return "notificationDraft";
			}
			
			onlinePath = notificationPreJobDataDTO.getOnlineDocumentDTO().getDocumentPath();
			onlineVersion = notificationPreJobDataDTO.getOnlineDocumentDTO().getDocumentVersion();
		} else {
			 onlinePath = notificationJobDataDTO.getOnlineDocumentDTO().getDocumentPath();
			 onlineVersion = notificationJobDataDTO.getOnlineDocumentDTO().getDocumentVersion();
		}
		 

		  if("0".equals(notificationType)) {
				if("预通知起草".equals(job.getJobType())) {
					return "notificationDraft";
				}
				notificationJobDataDTO = notificationPreJobDataDTO;
				if("预通知发送".equals(job.getJobType())) {
					 onlineIsEdit = "0,0";
					 List<UserInfoDTO> sendUsers = userManagementWebService.findAllUserInfo();
					 List<Contact> sendCantacts = iContactGroupService.findAllContactList();
					 List<UserInfoDTO> sendUsersWtihMail = new ArrayList<UserInfoDTO>();
				     if(sendUsers!=null && sendUsers.size()!=0){
				    	for(UserInfoDTO userInfo:sendUsers) {
				    		for(Contact contact:sendCantacts) {
				    			if(contact.getEmail()!=null) {
				    				if(contact.getUserInfoId().equals(userInfo.getUserInfoId())) 
					    			sendUsersWtihMail.add(userInfo);
					    		}
				    		}
				    		
				    	}
                        if(sendUsersWtihMail!=null&& sendUsersWtihMail.size()!=0) {
                        	sendUserListString = JSONArray.fromObject(sendUsersWtihMail).toString().replace("\"", "\\\"");
                        }
				        
				     }
					return "notificationSend";
				}
				
				if("预通知审批".equals(job.getJobType())) {
					return "notificationApprove";
				}
				if("预通知审批取回".equals(job.getJobType())) {
					return "notificationModify";
				}
				if("预通知修改".equals(job.getJobType())) {
					return "notificationModify";
				}
				if("预通知定稿".equals(job.getJobType())) {
					return "notificationModify";
				}
		  } else if("1".equals(notificationType)) {
			    if("正式通知起草".equals(job.getJobType())) {
					return "notificationDraft";
				}
			    notificationJobDataDTO = notificationPreJobDataDTO;
			    if("正式通知发送".equals(job.getJobType())) {
			   	 onlineIsEdit = "0,0";
				 List<UserInfoDTO> sendUsers = userManagementWebService.findAllUserInfo();
				 List<Contact> sendCantacts = iContactGroupService.findAllContactList();
				 List<UserInfoDTO> sendUsersWtihMail = new ArrayList<UserInfoDTO>();
			     if(sendUsers!=null && sendUsers.size()!=0){
			    	for(UserInfoDTO userInfo:sendUsers) {
			    		for(Contact contact:sendCantacts) {
			    			if(contact.getEmail()!=null) {
			    				if(contact.getUserInfoId().equals(userInfo.getUserInfoId())) 
				    			sendUsersWtihMail.add(userInfo);
				    		}
			    		}
			    		
			    	}
                    if(sendUsersWtihMail!=null&& sendUsersWtihMail.size()!=0) {
                    	sendUserListString = JSONArray.fromObject(sendUsersWtihMail).toString().replace("\"", "\\\"");
                    }
			        
			     }
					return "notificationSend";
				}

				if("正式通知审批".equals(job.getJobType())) {
					return "notificationApprove";
				}
				if("正式通知审批取回".equals(job.getJobType())) {
					return "notificationModify";
				}
				if("正式通知修改".equals(job.getJobType())) {
					return "notificationModify";
				}
				if("正式通知定稿".equals(job.getJobType())) {
					return "notificationModify";
				}
		  }
		return SUCCESS;
	}

//	private List<UserInfoDTO> getUserInfoWithNotDraftUserId(Long draftUserId) {
//		List<UserInfoDTO> userInfoApprovalList = new ArrayList<UserInfoDTO>();
//		userInfoApprovalList = userManagementWebService.findAllUserInfoHasAccount();
//		List<UserInfoDTO> users = new ArrayList<UserInfoDTO>();
//		if(userInfoApprovalList!=null&&userInfoApprovalList.size()!=0){
//			for(UserInfoDTO userInfoDto:userInfoApprovalList) {
//			   	if(!userInfoDto.getUserInfoId().equals(draftUserId)) {
//			   		users.add(userInfoDto);
//			   	}
//		}
//		
//		}
//		return users;
//		
//	}
			
	public NotificationJobDataDTO getNotificationJobDataDTO() {
		return notificationJobDataDTO;
	}

	public void setNotificationJobDataDTO(
			NotificationJobDataDTO notificationJobDataDTO) {
		this.notificationJobDataDTO = notificationJobDataDTO;
	}

	public JobDTO getJobDTO() {
		return jobDTO;
	}

	public void setJobDTO(JobDTO jobDTO) {
		this.jobDTO = jobDTO;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public List<JobDTO> getJobSchemeDTOs() {
		return jobSchemeDTOs;
	}

	public void setJobSchemeDTOs(List<JobDTO> jobSchemeDTOs) {
		this.jobSchemeDTOs = jobSchemeDTOs;
	}
    
  
	public String getNextApprovalListString() {
		return nextApprovalListString;
	}

	public void setNextApprovalListString(String nextApprovalListString) {
		this.nextApprovalListString = nextApprovalListString;
	}

	public List<UserInfoDTO> getNextApprovalList() {
		return nextApprovalList;
	}

	public void setNextApprovalList(List<UserInfoDTO> nextApprovalList) {
		this.nextApprovalList = nextApprovalList;
	}

	public String getNotificationType() {
		return notificationType;
	}

	public void setNotificationType(String notificationType) {
		this.notificationType = notificationType;
	}

	public List<JobDTO> getNotificationPreHistoryRecord() {
		return notificationPreHistoryRecord;
	}

	public void setNotificationPreHistoryRecord(
			List<JobDTO> notificationPreHistoryRecord) {
		this.notificationPreHistoryRecord = notificationPreHistoryRecord;
	}

	public List<JobDTO> getNotificationHistoryRecord() {
		return notificationHistoryRecord;
	}

	public void setNotificationHistoryRecord(List<JobDTO> notificationHistoryRecord) {
		this.notificationHistoryRecord = notificationHistoryRecord;
	}

	public String getOnlinePath() {
		return onlinePath;
	}

	public void setOnlinePath(String onlinePath) {
		this.onlinePath = onlinePath;
	}

	public String getOnlineVersion() {
		return onlineVersion;
	}

	public void setOnlineVersion(String onlineVersion) {
		this.onlineVersion = onlineVersion;
	}

	public List<JobDTO> getSchemeHistoryRecord() {
		return schemeHistoryRecord;
	}

	public void setSchemeHistoryRecord(List<JobDTO> schemeHistoryRecord) {
		this.schemeHistoryRecord = schemeHistoryRecord;
	}

	public JobDTO getJob() {
		return job;
	}

	public void setJob(JobDTO job) {
		this.job = job;
	}

	public String getSendUserListString() {
		return sendUserListString;
	}

	public void setSendUserListString(String sendUserListString) {
		this.sendUserListString = sendUserListString;
	}

	public String getOnlineIsEdit() {
		return onlineIsEdit;
	}

	public void setOnlineIsEdit(String onlineIsEdit) {
		this.onlineIsEdit = onlineIsEdit;
	}

	public ConferenceStartingDTO getConferenceStartingDTO() {
		return conferenceStartingDTO;
	}

	public void setConferenceStartingDTO(ConferenceStartingDTO conferenceStartingDTO) {
		this.conferenceStartingDTO = conferenceStartingDTO;
	}

	public List<JobDTO> getJobDTOs() {
		return jobDTOs;
	}

	public void setJobDTOs(List<JobDTO> jobDTOs) {
		this.jobDTOs = jobDTOs;
	}

    	
}
